package com.eg.thingscloud.example;

import com.alibaba.fastjson.JSONObject;
import com.eg.thingscloud.example.common.ApiUtil;
import com.eg.thingscloud.example.common.Constants;
import com.eg.thingscloud.example.common.HttpSignCaculate;
import com.eg.thingscloud.example.service.ThingscloudService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/**
 * 单门单卡连续时间段授权
 * @author geliangjian
 *
 */
public class PopedomODOCSetContinuousExample {

	public static void main(String[] args) {
		Gson gson = new GsonBuilder()
				.setLenient()
				.create();
		Retrofit retrofit = new Retrofit.Builder()
				.baseUrl(Constants.THINGSCLOUD_API_URL)
				.addConverterFactory(GsonConverterFactory.create(gson))
				.build();
		ThingscloudService service = retrofit.create(ThingscloudService.class);


		String accessToken = Constants.ACCESS_TOKEN;
		String timestamp = ApiUtil.getTimeStamp();
		Integer signMethod = Constants.SIGN_METHOD_HMAC;
		String doorId = "f5b661672eb24b039ecd222834d0d5df";
		Long cardNumber = 459540545L;
		String startDateTime = "2017-03-17 08:00:00";
		String endDateTime = "2017-03-17 22:00:00";
		String weekOption = "";

		Map<String, Object> signParams = new HashMap<String, Object>();
		signParams.put("access_token", accessToken);
		signParams.put("timestamp", timestamp);
		signParams.put("sign_method", signMethod);
		signParams.put("door_id", doorId);
		signParams.put("card_number", cardNumber);
		signParams.put("start_datetime", startDateTime);
		signParams.put("end_datetime", endDateTime);
		signParams.put("door_id", doorId);
		if(weekOption != null && !"".equalsIgnoreCase(weekOption)){
			signParams.put("week_option",weekOption);
		}

		String sign = null;
		try {
			sign = HttpSignCaculate.signEIOTRequest(signParams,Constants.SECRET_KEY,signMethod);
		} catch (IOException e1) {
			e1.printStackTrace();
		} catch (NoSuchAlgorithmException e2) {
			e2.printStackTrace();
		}

		Call<String> call = service.continuousCardOpenRightAdd(accessToken,timestamp,signMethod,sign,doorId,cardNumber,
				startDateTime,endDateTime,weekOption);
		try {
			Response<String> repos  = call.execute();
			System.out.println(repos);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
